package org.grow.present.controller.auth;



import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.grow.present.dto.PwdRegUserInfo;
import org.grow.present.entity.UserInfo;
import org.grow.present.entity.UserPwdLogin;
import org.grow.present.exception.BillStateException;
import org.grow.present.mapper.UserInfoMapper;
import org.grow.present.mapper.UserPwdLoginMapper;
import org.grow.present.service.AuthPasswordEncoder;
import org.grow.present.exception.MissTargetException;
import org.grow.present.interceptor.AuthInterceptor;
import org.grow.present.service.CodeGenerator;
import org.grow.present.service.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xwg
 * @CreateDate: 2023/9/21
 */
@Slf4j
@RestController
@RequestMapping("/pwd")
public class PwdLoginController {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserPwdLoginMapper userPwdLoginMapper;
    @Autowired
    private AuthPasswordEncoder authPasswordEncoder;
    @Autowired
    private JwtService jwtService;
    @Autowired
    private CodeGenerator codeGenerator;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password) {
//    step1  check
        UserPwdLogin userPwdLogin = userPwdLoginMapper.findByUsername(username);
        if (userPwdLogin == null) {
            throw new MissTargetException("用户名不存在");
        }
        if (!userPwdLogin.getEnable()) {
            throw new MissTargetException("帐号被冻结");
        }
        String passwordFromDB = userPwdLogin.getPassword();
        String passwordFromFront = authPasswordEncoder.encode(password);
        if (!passwordFromFront.equals(passwordFromDB)) {
            throw new MissTargetException("用户名密码不匹配!!!!");
        }
        log.info("用户"+userPwdLogin.getUID()+"登录成功");
        UserInfo userInfo = userInfoMapper.findById(userPwdLogin.getUID());
        if (userInfo == null) {
            throw new BillStateException("登录正确，没有对应资料");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("UID", userInfo.getUserId());
        String token = jwtService.createToken(map);
        HashMap<String, Object> returned = new HashMap<>();
        returned.put("token", token);
        return returned;

    }


    @PostMapping("/register")
    public String register(@RequestBody PwdRegUserInfo pwdRegUserInfo){
        String username = pwdRegUserInfo.getUsername();
        UserPwdLogin u = userPwdLoginMapper.findByUsername(username);
        if (u != null){
            throw new MissTargetException("User " + username + "登录名已经存在");
        }
        UserPwdLogin userPwdLogin = new UserPwdLogin();
        UserInfo userInfo = new UserInfo();
        String UID = codeGenerator.nextCode("USE");
        userPwdLogin.setUID(UID);
        userPwdLogin.setEnable(true);
        userPwdLogin.setUsername(pwdRegUserInfo.getUsername());
        userPwdLogin.setPassword(authPasswordEncoder.encode(pwdRegUserInfo.getPassword()));
        userInfo.setNickName(pwdRegUserInfo.getNickname());
        userInfo.setUserId(UID);
        userInfo.setBalance(new BigDecimal(100));
        userInfo.setMenus("1,5");
        userInfo.setPic("");
        userInfoMapper.insert(userInfo);
        userPwdLoginMapper.insert(userPwdLogin);
        return "注册成功";
    }


}
